package com.shiliu.api.common.exception;

import com.shiliu.model.pojo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description: 全局异常处理器
 * @author: shiliu
 * @created: 2019/12/22 14:46
 * @version: v1.0.0
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 业务异常拦截
     */
    @ExceptionHandler(ServiceException.class)
    public ResponseResult serviceException(ServiceException e){
        log.error("业务层异常: " + e.getMessage(),e);
        return ResponseResult.error(e.getMessage());
    }

    /**
     * 服务器错误异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult handleException(Exception e){
        log.error("服务器错误: " + e.getMessage(),e);
        return ResponseResult.error500("服务器错误，请联系管理员!!!");
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public ResponseResult notFount(RuntimeException e){
        log.error("运行时异常:", e);
        return ResponseResult.error("运行时异常:" + e.getMessage());
    }

    /**
     * 请求方式不支持
     */
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    public ResponseResult handleException(HttpRequestMethodNotSupportedException e){
        log.error(e.getMessage(), e);
        return ResponseResult.error("不支持' " + e.getMethod() + " '请求");
    }
}
